import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 苏李涛
 * Date: 2024-10-24
 * Time: 17:16
 */

//多线程原子类的使用

public class Demo {
    private static AtomicInteger count = new AtomicInteger();

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(()->{
            for (int i = 0; i < 50000; i++) {

                //`getAndIncrement()` 会原子性地将当前值增加 1
                count.getAndIncrement();
            }
        });

        Thread t2 = new Thread(()->{
            for (int i = 0; i < 50000; i++) {

                //`getAndIncrement()` 会原子性地将当前值增加 1
                count.getAndIncrement();
            }
        });

        t1.start();
        t2.start();

        t1.join();
        t2.join();

        //`get()` 获取当前值。
        System.out.println(count.get());

    }
}
